package com.briup.service.impl;

import com.briup.bean.PageBean;
import com.briup.dao.BookDao;
import com.briup.dao.CategoryDao;
import com.briup.dao.extend.BookExtendDao;
import com.briup.exception.ServiceException;
import com.briup.pojo.Book;
import com.briup.pojo.Category;
import com.briup.pojo.extend.BookExtend;
import com.briup.result.ResultCode;
import com.briup.service.BookService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookExtendDao bookExtendDao;
    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private BookDao bookDao;

    @Override
    public List<BookExtend> getBooksByCondition(String keyword, Integer categoryId) {
         List<BookExtend> list =bookExtendDao.selectBooksByCondition(keyword, categoryId);
        return list;
    }

    @Override
    public void addBook(Book book) {
        //参数校验 验证必填项 验证参数类型等内容
        validate(book);

        //验证分类信息是否存在
        Integer categoryId = book.getCategoryId();
        if (categoryId != null){
            categoryIsOrNotExist(categoryId);
        }
        //添加操作了
        bookDao.insertBook(book);
    }

    private void categoryIsOrNotExist(Integer categoryId) {
        categoryDao.selectCategoryById(categoryId);
        if (categoryId == null){
            throw new ServiceException(ResultCode.CATEGORY_IS_NOT_EXIST);
        };
    }

    private void validate(Book book) {
        if(!StringUtils.hasText(book.getName())){
            throw new ServiceException(ResultCode.BOOK_NAME_IS_NULL);
        };
        if(!StringUtils.hasText(book.getAuthor())){
            throw new ServiceException(ResultCode.BOOK_AUTHOR_IS_NULL);
        };
        if(!StringUtils.hasText(book.getPublisher())){
            throw new ServiceException(ResultCode.BOOK_PUBLISHER_IS_NULL);
        };

    }

    @Override
    public void modifyBook(Book book) {
        // 参数校验 验证必填项 验证参数类型等内容
        validate(book);

        // 确保图书存在
        if (book.getId() == null ) {
            throw new ServiceException(ResultCode.BOOK_IS_NOT_EXIST);
        }

        // 验证分类信息是否存在
        Integer categoryId = book.getCategoryId();
        if (categoryId != null) {
            categoryIsOrNotExist(categoryId);
        }

        try {
            bookDao.updateBook(book);
            bookDao.clearShopCarByBookId(book.getId());
        } catch (DataAccessException e) {
            throw new ServiceException(ResultCode.BOOK_IS_NOT_EXIST);
        }
    }

    @Override
    public PageBean getBooksByConditionAndPage(String keyword, Integer categoryId, Integer pageNum, Integer pageSize) {

        // 计算开始的索引
        Integer pageIndex = (pageNum - 1 ) * pageSize;
        // 查询数据
        List<BookExtend> books = bookExtendDao.selectBooksByConditionAndPage(keyword, categoryId, pageIndex, pageSize);
        // 查询满足条件的书籍总数
        Long total = bookExtendDao.selectCount(keyword, categoryId);
        PageBean pageBean = new PageBean(total,books);
        return pageBean;
    }





//    ---------------------------------------------------------------------------------

    @Override
    public void removeBookById(Integer id) {
        bookDao.deleteBookById(id);

    }

    @Override
    public void modifyBookStatus(Integer id, Integer status) {
        bookDao.updateBookStatus(id, status);
    }

    @Override
    public Book getBookById(Integer id) {
        Book bookById = bookDao.selectBookById(id);
        return bookById;
    }



    @Override
    public void removeBatchByIds(List<Integer> ids) {

        bookDao.deleteBookByIds(ids);
        bookDao.clearShopCarByBookIds(ids);

    }
    @Override
    public PageInfo getBooksByConditionAndPageNew(String keyword, Integer categoryId, Integer pageNum, Integer pageSize) {
        //开启分页查询 直接传入页码和每页条数
        PageHelper.startPage(pageNum, pageSize);
        //查询数据 不需要分页查询 使用普通方法就行
        List<BookExtend> bookExtends = bookExtendDao.selectBooksByCondition(keyword, categoryId);
        //封装成PageInfo对象
        PageInfo<BookExtend> pageInfo = new PageInfo<>(bookExtends);
        return pageInfo;

    }

}
